﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Job.Web.Framework;
using Job.Logic.Personal;
using Job.Entity;

namespace Job.Web.userControls.Personal
{
    public partial class WorkExperience : BaseCtrl
    {
        int workid = myR["pwid"].IntValue;
        int subjectid = myR["subjectid"].IntValue;
        int pid = myR["resumeid"].IntValue;
        PersonalLogic pl = new PersonalLogic();
        JobPersonWork work = new JobPersonWork();
        private void ShowMessage(string message)
        {
            this.message.InnerHtml = "<font color=red>" + message + "</font>";
            this.message.Visible = true;
            this.message.Height = "30px";
            this.message.VAlign = "middle";
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                this.InitData();
                this.LoadData();
            }
        }

        private void InitData()
        {
            //初始化月份
            ddlFMonth.Items.Clear();
            ddlFYear.Items.Clear();
            ddlTYear.Items.Clear();
            ddlTMonth.Items.Clear();

            for (int i = 1; i <= 12; i++)
            {
                ddlFMonth.Items.Add(new ListItem(i.ToString(), i.ToString()));
                ddlTMonth.Items.Add(new ListItem(i.ToString(), i.ToString()));
            }

            //初始化年份
            for (int i = 1949; i <= DateTime.Now.Year; i++)
            {
                //ListItem item = new ListItem(i.ToString(), i.ToString());
                ddlFYear.Items.Add(new ListItem(i.ToString(), i.ToString()));
                ddlTYear.Items.Add(new ListItem(i.ToString(), i.ToString()));
            }

            ddlTMonth.Items.Insert(0,new ListItem("至今","-1"));
            ddlTYear.Items.Insert(0,new ListItem("至今", "-1"));
        }

        private void LoadData()
        {
            try
            {
                gvWork.DataSource = pl.GetWorksByPersonalId(pid);
                gvWork.DataBind();

                if (workid > 0)
                {
                    work = pl.GetSingleWork(workid);
                    if (work != null)
                    {
                        txtCompanyName.Text = work.CompanyName;
                        txtPositionName.Text = work.PositionName;
                        txtWorkDesc.Text = work.WorkDesc;
                        if (work.StartYear.HasValue)
                        {
                            if (ddlFYear.Items.FindByValue(work.StartYear.Value.ToString()) != null)
                            {
                                ddlFYear.SelectedValue = work.StartYear.Value.ToString();
                            }
                        }

                        if (work.StartMonth.HasValue)
                        {
                            if (ddlFMonth.Items.FindByValue(work.StartMonth.Value.ToString()) != null)
                            {
                                ddlFMonth.SelectedValue = work.StartMonth.Value.ToString();
                            }
                        }

                        if (work.EndYear.HasValue)
                        {
                            if (ddlTYear.Items.FindByValue(work.EndYear.Value.ToString()) != null)
                            {
                                ddlTYear.SelectedValue = work.EndYear.Value.ToString();
                            }
                        }

                        if (work.EndMonth.HasValue)
                        {
                            if (ddlTMonth.Items.FindByValue(work.EndMonth.Value.ToString()) != null)
                            {
                                ddlTMonth.SelectedValue = work.EndMonth.Value.ToString();
                            }
                        }

                        txtManageCount.Text = work.ManageCount.HasValue == true ? work.ManageCount.Value.ToString() : "";
                        txtRef.Text = work.Reference;
                        txtReportTo.Text = work.ReportTo;
                        txtReson.Text = work.LeaveReason;
                        txtResult.Text = work.Result;

                    }//end work = null
                    lblMode.Text = "编辑工作经验";
                }//end work id >0
                else
                {
                    lblMode.Text = "新增工作经验";
                }
            }
            catch (Exception ex)
            {
                Logger.WriteLog("工作经验查询失败:\r\n" + ex.Message);
                ShowMessage("查询失败:\r\n" + ex.Message);
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (work == null)
                {
                    work = new JobPersonWork();
                }

                work.CompanyName = txtCompanyName.Text.Trim();
                work.PositionName = txtPositionName.Text.Trim();
                work.WorkDesc = txtWorkDesc.Text.Trim();

                work.StartYear = Convert.ToInt32(ddlFYear.SelectedValue);
                work.StartMonth = Convert.ToInt32(ddlFMonth.SelectedValue);
                work.EndYear = Convert.ToInt32(ddlTYear.SelectedValue);
                work.EndMonth = Convert.ToInt32(ddlTMonth.SelectedValue);
                work.PersonInfoId = pid;

                if (txtResult.Text.Trim().Length > 0)
                {
                    work.Result = txtResult.Text;
                }
                else
                {
                    work.Result = "";
                }
                if (txtReson.Text.Trim().Length > 0)
                {
                    work.LeaveReason = txtReson.Text;
                }
                else
                {
                    work.LeaveReason = "";
                }

                if (txtReportTo.Text.Trim().Length > 0)
                {
                    work.ReportTo = txtReportTo.Text;
                }
                else
                {
                    work.ReportTo = "";
                }

                if (txtRef.Text.Trim().Length > 0)
                {
                    work.Reference = txtRef.Text;
                }
                else
                {
                    work.Reference = "";
                }

                if (txtManageCount.Text.Trim().Length > 0)
                {
                    work.ManageCount = Convert.ToInt32(txtManageCount.Text.Trim());
                }
                else
                {
                    work.ManageCount = null;
                }

                if (workid > 0)
                {
                    work.PersonWorkId = workid;
                    pl.UpdateWork(work);
                }
                else
                {
                    work.PersonInfoId = pid;
                    pl.InsertWork(work);
                }
            }
            catch (Exception ex)
            {
                Logger.WriteLog("工作经验保存失败:\r\n" + ex.Message);
                ShowMessage("保存失败:\r\n" + ex.Message);
                return;
            }

            string url = WebCommon.GetApplicationPath(Request) + "Person/PersonalResume.aspx?categoryid=1&subjectid=" + this.subjectid + "&resumeid=" + this.pid;
            Response.Redirect(url);
        }

        protected void gvWork_RowCommand(object sender, GridViewCommandEventArgs e)
        {
             int id =Convert.ToInt32( e.CommandArgument);
            if (e.CommandName.ToLower() == "edit")
            {
                //edit
                string url = WebCommon.GetApplicationPath(Request) + "Person/PersonalResume.aspx?categoryid=1&subjectid=" + this.subjectid + "&resumeid=" + this.pid + "&pwid=" + id;
                Response.Redirect(url);

            }
            else if (e.CommandName.ToLower() == "delete")
            {
                JobPersonWork _work = pl.GetSingleWork(id);
                pl.DeleteWork(_work);

                string url = WebCommon.GetApplicationPath(Request) + "Person/PersonalResume.aspx?categoryid=1&subjectid=" + this.subjectid + "&resumeid=" + this.pid;
                Response.Redirect(url);
            }

        }

        protected void gvWork_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {

        }
    }
}